/*
 * esoteric_interpreter.cpp
 *
 *  Autor: Andrej Palicka
 *  Skupina: I32
 *  Zadanie: interpreter jazyka brainfuck
 *
 */

#include <fstream>
#include <unistd.h>
#include <string>
#include "parser.h"
#include <cstdlib>
#include <cstring>

const int SZ = 30000;
int main(int argc, char* argv[]) {
	int size = SZ;
	char opt;
	std::ofstream oFile;
	bool convert = false;
	/*
	 * Kontrola jednotlivych argumentov.
	 * Mozne argumenty:
	 * -f [file] - subor z ktoreh sa nacita program
	 * -s [size] - velkost datoveho pola, defaultne je 30000
	 * -c [file] - v pripade, ze chceme program konvertovat na zdrojak v cecku, vysledok sa ulozi vo [file]
	 */
	std::ifstream iFile;
	while((opt = getopt(argc, argv, "f:s:c:d")) != -1) {
		switch(opt) {
		case 'f':
			iFile.open(optarg);
			break;
		case 's':
			size = atoi(optarg);
			break;

		case 'c':
			oFile.open(optarg, std::ios_base::out);
			convert = true;
			break;
		}
	}
	std::string line;
	std::string code;
	while(!iFile.eof()) {
		std::getline(iFile, line);
		for(int i = 0; i < line.size(); i++) {
			if(line[i] == '+' || line[i] == '-' || line[i] == '<' || line[i] == '>'
					|| line[i] == '[' || line[i] == ']' || line[i] == '.' || line[i] == ',') {
				code.push_back(line[i]);
			}
		}

	}
	//zvoli moznost intepretacie kodu, alebo jeho konvertovania do C
	if(!convert) {
		parseCode(code, size);
	} else {
		convertToC(code, oFile, size);
	}
	return 0;
}
